package org.example.projecttwo.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Delete;
import org.example.projecttwo.common.Result;
import org.example.projecttwo.entity.Role;
import org.example.projecttwo.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
@Tag(name = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    @Operation(summary = "分页查询角色列表")
    public Result<IPage<Role>> listRole(
            @Parameter(description = "名称查询") @RequestParam(required = false) String keyword,
            @Parameter(description = "状态筛选") @RequestParam(required = false) Integer status,
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10") long size) {
        Page<Role> page = new Page<>(current, size);
        IPage<Role> result = roleService.pageBy(page, keyword, status);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取角色详情")
    public Result<Role> getRole(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @PostMapping
    @Operation(summary = "新增角色")
    public Result<Role> createRole(@Valid @RequestBody Role role) {
        roleService.save(role);
        return Result.success(role);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改角色")
    public Result<Role> updateRole(@PathVariable Long id, @Valid @RequestBody Role role) {
        Role existing = roleService.getById(id);
        if (existing == null) {
            return Result.error("角色不存在");
        }
        if (existing.getStatus() == 0) {
            return Result.error("已启用角色不可编辑，请先禁用");
        }
        role.setId(id);
        roleService.updateById(role);
        return Result.success(role);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<String> deleteRole(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.success("删除成功");
    }

    @PutMapping("/{id}/status-switch")
    @Operation(summary = "切换角色状态", description = "0-启用，1-禁用")
    public Result<String> switchRoleStatus(@PathVariable Long id) {
        Role role = roleService.getById(id);
        if (role == null) {
            return Result.error("角色不存在");
        }
        role.setStatus(role.getStatus() == 0 ? 1 : 0);
        roleService.updateById(role);
        return Result.success("切换成功");
    }

    @PostMapping("/batch-delete")
    @Operation(summary = "批量删除角色")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择至少一条角色");
        }
        roleService.removeByIds(ids);
        return Result.success("批量删除成功,共" + ids.size() + "条");
    }

    @PostMapping("/batch-start-status")
    @Operation(summary = "批量启用角色", description = "status = 0")
    public Result<String> batchStartStatus(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择至少一条角色");
        }
        roleService.batchUpdateStatus(ids, 0);
        return Result.success("批量启用成功,共" + ids.size() + "条");
    }

    @PostMapping("/batch-forbidden-status")
    @Operation(summary = "批量禁用角色", description = "status = 1")
    public Result<String> batchForbiddenStatus(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择至少一条角色");
        }
        roleService.batchUpdateStatus(ids, 1);
        return Result.success("批量禁用成功,共" + ids.size() + "条");
    }

    @PostMapping("/batch-export")
    @Operation(summary = "批量导出角色")
    public Result<String> batchExport(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择至少一条角色");
        }
        // 导出逻辑
        return Result.success("批量导出成功,共" + ids.size() + "条");
    }
}
